package com.labmi.experiment;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;


/**
 * <A name="non-persistent"/>
 * A non-persistent entity holds the content of a shopping session for a {@linkplain Customer}.
 * Used to create a persistent PurchaseOrder.
 * Items can be added or removed from this cart.
 * 
 * @author Pinaki Poddar
 *
 */
public class ShoppingCart implements Serializable {
	
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -8880964374935575917L;
	/**
     * <A name="items"/>
     * The items in the cart and their respective quantity.
     */
    private Map<Item, Integer> items;
    
//    /**
//     * Construct a cart for the given Customer.
//     *  
//     * @param c non-null Customer.
//     */
//    ShoppingCart(Customer c) {
//        customer = c;
//        items = new HashMap<Item, Integer>();
//    }
		public ShoppingCart() {
			items = new HashMap<Item, Integer>();
		}
//    /**
//     * Gets the Customer who owns this cart.
//     * 
//     * @return non-null Customer.
//     */
//    public Customer getCustomer() {
//        return customer;
//    }
    
    /**
     * Gets the items with their corresponding quantity in this cart.
     * 
     * @return
     */
    public Map<Item, Integer> getItems() {
        return items;
    }
    
    /**
     * Add the given book with the given quantity in the cart.
     * If the book already exists then the quantity is added to the existing quantity.
     * 
     * @param item non-null Item
     * @param quantity a positive quantity.  
     */
    public void addItem(Item item, int quantity) {
        if (item == null)
            throw new NullPointerException("Can not add null Item to " + this);
        if (quantity < 1)
            throw new IllegalArgumentException("Can not add " + quantity + " " + item + " to " + this + 
               " Added qunatity must be greater than zero");
        int current = items.containsKey(item) ? items.get(item) : 0;
        items.put(item, current + quantity);
    }
    
    
    /**
     *
     * 
     * @param item non-null Item
     *   
     */
    public int getItemQuantity(Item item) {
        if (item == null)
            throw new NullPointerException("Can not add null Item to " + this);
        
        return items.get(item);
    }
    
    /**
     * Change the quantity of the given book by the given delta.
     * 
     * @param item a non-null Item that must exist in this cart.
     * @param delta no. of quantity to change. Can be positive or negative.
     * If the resultant quantity becomes zero of negative, the book is removed from the cart.
     */
    public void changeQuantity(Item item, int delta) {
        if (item == null)
            throw new NullPointerException("Can not change quantity for null Item in " + this);
        if (!items.containsKey(item))
            throw new IllegalArgumentException("Can not change quantity for " + item + " becuase the book does not " +
                    "exist in " + this);
        int current = items.containsKey(item) ? items.get(item) : 0;
        if (current + delta <= 0) {
            items.remove(item);
        } else {
            items.put(item, current + delta);
        }
    }
    
    /**
     * Removes the given book from this cart.
     * 
     * @param item book a non-null Item that must exist in this cart.
     */
    public void remove(Item item) {
        if (item == null)
            throw new NullPointerException("Can not remove null Item from " + this);
        if (!items.containsKey(item))
            throw new IllegalArgumentException("Can not remove " + item + " becuase the book does not " +
                    "exist in " + this);
        items.remove(item);
    }
    
    public void clear() {
        items.clear();
    }
    
    public boolean isEmpty() {
        return items.isEmpty();
    }
    
    public int getSize() {
        return items.size();
    }
    
    
    
    public int getTotalCount() {
        int sum = 0;
        for (Integer q : items.values())
            sum += q.intValue();
        return sum;
    }

    public int getTotalCost() {
    	Set<Item> keySet = items.keySet();
    	int tempPrice = 0; 
    	for (Item item : keySet) {
			Integer itemQuantity = items.get(item);
			tempPrice = tempPrice + (item.getPrice() * itemQuantity);
		}
    	return tempPrice;
    }
    
    
    
}
